<template>
  <div style="height: 100%;">
    <el-container>
      <el-header>
        <div class="flex">
          <div class="flex">
            <div @click="tab" class="tab"><i style="font-size: 35px;"
                :class="isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"></i></div>
            <div class="flex">
              <div class="logo">
                <img src="../assets/image/logo.png" alt="">
              </div>
              <div class="size_20" style="color: #fff;">绣娘丝绸后台管理系统</div>
            </div>
          </div>
          <div>
            <Tabtitle @tabtitle="tabtitle"></Tabtitle>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '0px' : '210px'">
          <Menu :isCollapse="isCollapse"></Menu>
        </el-aside>
        <el-main>
          <div class="bread_card" v-if="breadcrumbList.length != 0">
            <!--  -->
            <el-button icon="el-icon-arrow-left" size="small" style="margin-right: 10px;" @click="backToPrevpage">返回</el-button>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item :to="item.path" v-for="(item, index) in breadcrumbList" :key="index">{{ item.meta.title
              }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div style="padding:0 10px;">
            <keep-alive>
              <router-view v-if="keepAlive"></router-view>
            </keep-alive>
            <router-view v-if="!keepAlive"></router-view>
          </div>
          <customer_service></customer_service>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import Menu from '@/components/Main/Menu.vue'
import Tabtitle from '@/components/Tabtitle.vue'
export default {
  components: { Menu, Tabtitle },
  data() {
    return {
      isCollapse: false,
      breadcrumbList: [],
      keepAlivelist: ['/consumergroup_add', '/product_copy_index', '/shop_copy_index', '/ride_copy_index', '/overall_portrait_select', 'Portrait_comparison','/purchase_intention']
    }
  },
  computed: {
    // menulist() {
    //   return this.$store.state.common.Menus;
    // },
    // defaultOpened() {
    //   let ary = ['100']
    //   this.$store.state.common.Menus.forEach(it => {
    //     ary.push(it.id)
    //   })
    //   return ary;
    // },
    keepAlive() {
      return this.keepAlivelist.some(item => item === this.$route.fullPath)
    }
  },
  watch: {
    $route: {
      handler(newVal) {
        let routerlist = JSON.parse(localStorage.getItem("MenuList"))
        let ary = this.recursion(
          routerlist,
          this.$router.history.current.meta.title
        );
        if (ary.length == 0) {
          console.log(ary, 123)
          this.breadcrumbList = [
            {
              meta: {
                title: sessionStorage.getItem("routitle")
              }
            },
            {
              meta: {
                title: this.$router.history.current.meta.title
              }
            }
          ]
        } else {
          sessionStorage.setItem("routitle", ary[0].meta.title);
          this.breadcrumbList = ary
        }
      },
      // immediate: true
    }
  },
  created() {
    this.$store.dispatch("getmenuslist");
    this.$store.dispatch("getUserinfo");
  },
  mounted() {
    if (this.$router.history.current.meta.title != "首页") {
      this.$nextTick(() => {
        this.breadcrumbList = [
          {
            meta: {
              title: sessionStorage.getItem("routitle")
            }
          },
          {
            meta: {
              title: this.$router.history.current.meta.title
            }
          }
        ]
      })
    }
  },
  computed: {},
  methods: {
    tab() {
      this.isCollapse = !this.isCollapse
    },
    tabtitle(val) {
      console.log(val, 123)
    },
    recursion(map, key) {
      let t = [];
      for (let i = 0; i < map.length; i++) {
        const e = map[i];
        if (e.meta.title === key) {
          t.push(e);
          break;
        } else if (e.children && e.children.length !== 0) {
          //判断是否还有子节点，若有对子节点进行循环调用
          let p = this.recursion(e.children, key);
          //若p的长度不为0，则说明子节点在该节点的children内，记录此节点，并终止循环
          if (p.length !== 0) {
            p.unshift(e);
            t = p;
            break;
          }
        }
      }
      return t;
    },
  },
}
</script>
<style lang="less" scoped>
.tab {
  width: 64px;
  height: 60px;
  font-size: 30px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tab:hover {
  background: #fff;
  color: #314790;
}

.logo {
  height: 30px;
  display: inline-block;
  overflow: hidden;
  margin-right: 15px;

  img {
    width: 100%;
    height: 100%;
  }
}

.find_width {
  width: 210px;
  transition: all 0.3s;
  // z-index: 10;
}

.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 整体 */

.main_box {
  height: 100%;
}

// 
.el-header,
.el-footer {
  background-color: #314790;
  padding: 0 10px 0 0;

}

.el-aside {
  background-color: #304156;
  color: #333;
  text-align: center;
  line-height: 200px;
  transition: all .5s;
}

.el-main {
  color: #333;
  padding: 0;
}

.el-container {
  height: 100%;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.bread_card {
  padding: 10px 10px;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  display: flex;
  align-items: center;
}
</style>
